package com.library.common.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import com.library.common.util.DensityUtil.dip2px

/**
 * Created by DON on 2017/8/22.
 */
class RoundImageView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatImageView(context!!, attrs, defStyleAttr) {
    private var radius: Int = dip2px(5f)
    fun setRadius(radius: Int) {
        this.radius = radius
    }

    override fun onDraw(canvas: Canvas) {
        val path = Path()
        path.addRoundRect(
            RectF(0f, 0f, width.toFloat(), height.toFloat()),
            radius.toFloat(),
            radius.toFloat(),
            Path.Direction.CW
        )
        canvas.clipPath(path) //设置可显示的区域，canvas四个角会被剪裁掉
        super.onDraw(canvas)
    }
}